<template>
	<view class="page">
		<view class="uni-list">
			<block v-if="listData.length>0">
				<view class="uni-list-cell" hover-class="uni-list-cell-hover" v-for="(value,key) in listData" :key="key" @click="goDetail(value)"
				 @longpress="show_m(value)">
					<view class="uni-media-list">
						<image class="uni-media-list-logo" :src="value.face"></image>
						<view v-if="value.stotal>0" class="tip">{{value.stotal}}</view>
						<view class="uni-media-list-body">
							<view class="uni-media-list-text-top"  >{{value.cont}}</view>
							<view class="uni-media-list-text-bottom">
								<block v-if="value.type=='agent'">
									<block v-for="(s,k) in agentarr" :key="k">
										<text v-if="value.id==s.id">{{s.name}}</text>
									</block>
								</block>
								<block v-else>
									<text>{{value.name}}</text>
								</block>
								<text>{{value.optdts}}</text>
							</view>
						</view>
					</view>
				</view>
			</block>
			<block v-else>
				<view class="page-share-title">
					<text>暂无数据</text>
				</view>
			</block>
 
		</view>
	</view>
</template>
<script>
	import {
		Base64
	} from '../../utils/base64.min.js';
	import service from '../../utils/service.js';
 
	export default {
		data: {
			listData:'',
			agentarr: '',
			Loop: '',
			ck: true,
			provider: '',
			cont:''
		},
 
		onLoad() {
			if (!uni.getStorageSync('$APPLOGIN')) {
				uni.reLaunch({
					url: '../login/login'
				});
				return;
			}
			
			this.get_reimarr();
			this.listenTranMsg();
		},
		onShow() {
			//this.get_reimarr();
		},
		methods: {
			listenTranMsg: function() {
				var that = this;
				var provider=uni.getStorageSync('$provider');
				uni.subscribePush({
					provider: provider[0],
					success: (e) => {
// 						uni.showToast({
// 							title: "已开启push接收"
// 						})
					}
				});
				uni.onPush({
					provider: provider[0],
					success: (e) => {
// 						uni.showToast({
// 							title: "开始监听透传数据"
// 						})
					},
					receive: (e) => {
						//that.get_reimarr()
					},
					
				})
				
			},
 
			get_reimarr: function() {
				uni.showLoading({
					title: '加载中...'
				});
				const data = uni.getStorageSync('$APPLOGIN');
				var that = this;
				service.getURL('?m=index&a=indexapp',data,function(res){
					if (res.data.data.reimarr.length > 0) {
						var da = res.data.data.reimarr;
						that.listData = da;
						that.agentarr = res.data.data.agentarr;
					}
				})
				uni.hideLoading();
			},
			show_m: function(value) {
 
				var that = this;
				uni.showActionSheet({
					itemList: ['删除记录', '取消'],
					success: function(res) {
						if (res.tapIndex == 0) {
							uni.showModal({
								title: '提示',
								content: '确定要删除会话记录吗?',
								success: function(res) {
									if (res.confirm) {
										const server ='?m=reim&a=delhistory&gid=' + value.receid + '&type=' + value.type;
										const data = uni.getStorageSync('$APPLOGIN');
										
										service.getURL(server,data,function(res){
											
											that.get_reimarr();
											uni.showToast({
												title: '删除成功！',
												duration: 2000
											});
											
										})
									}
								}
							});
						}
 
					}
				});
			},
			goDetail: function(value) {
				var agentarr = this.agentarr;
 
 
				if (value.type == 'user') {
					uni.navigateTo({
						url: 'main-msg?value=' + JSON.stringify(value)
					})
				}
				if (value.type == 'group') {
					uni.showToast({
						title: '没有',
						duration: 2000
					});
				}
				if (value.type == 'agent') {
					for (var i = 0; i < agentarr.length; i++) {
						if (value.receid === agentarr[i]['id']) {
							value.num = agentarr[i]['num'];
							break;
						}
					}
					uni.navigateTo({
						url: 'main-agent?value=' + JSON.stringify(value)
					})
				}
			},
			clearLoop: function() {
				clearInterval(this.Loop);
			}
 
		}
	}
</script>
 
<style>
	.tip {
		min-width: 30upx;
		height: 30upx;
		background: red;
		box-sizing: border-box;
		color: white;
		font-size: 16upx;
		text-align: center;
		line-height: 30px;
		padding: 0 0;
		border-radius: 30px;
 
		position: absolute;
		left: 85upx
	}
 
	.uni-media-list-logo {
		width: 80upx;
		height: 80upx;
		border-radius: 40upx;
	}
 
	.uni-media-list-body {
		height: auto;
		justify-content: space-around;
	}
 
	.uni-media-list-text-top {
		height: 50upx;
		font-size: 28upx;
		overflow: hidden;
		text-overflow: ellipsis;
		white-space: nowrap;
	}
 
	.page-share-title {
		text-align: center;
		font-size: 30upx;
		color: #BEBEBE;
		padding: 20upx 0;
	}
 
	.uni-media-list-text-bottom {
		display: flex;
		flex-direction: row;
		justify-content: space-between;
	}
</style>